package com.yisu.expressway.manager.website.controller.goodsArchives;

import com.google.common.collect.Lists;
import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.CategoryBanner;
import com.yisu.expressway.ewf.entity.GoodsArchives;
import com.yisu.expressway.manager.core.service.ICategoryBannerService;
import com.yisu.expressway.manager.core.service.IGoodsCategory;
import com.yisu.expressway.manager.core.service.IRecommendGoodsService;
import com.yisu.expressway.manager.domain.request.CategoryBannerRequest;
import com.yisu.expressway.manager.domain.request.GoodsArchivesRequest;
import com.yisu.expressway.manager.domain.vo.RecommendGoodsForSelVo;
import com.yisu.expressway.manager.website.controller.BaseController;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.FAILURE;

/**
 * Created by xlt on 2017/4/26.
 */
@Controller
@RequestMapping(value = "/categoryBanner")
public class CategoryBannerController extends BaseController {
    @Resource
    private IGoodsCategory iGoodsCategory;
    @Resource
    private ICategoryBannerService iCategoryBannerService;
    @Resource
    private IRecommendGoodsService iRecommendGoodsService;

    @RequestMapping(value = "/index.htm")
    public String listPage(Model model, CategoryBannerRequest categoryBannerRequest) {

        String pathNodeName = "";
        if (!StringUtils.isEmpty(categoryBannerRequest.getCategoryCode())) {
            pathNodeName = iGoodsCategory.getGoodsCategoryPathNameByGoodsCategoryCode(categoryBannerRequest.getCategoryCode());
        }
        List<RecommendGoodsForSelVo> recommendGoodsGoodsArchives=iRecommendGoodsService.getRecommendGoodsGoodsArchivesByCategoryCode(categoryBannerRequest.getCategoryCode());
        if(CollectionUtils.isEmpty(recommendGoodsGoodsArchives))
            recommendGoodsGoodsArchives= Lists.newArrayList();
        model.addAttribute("recommendGoodsGoodsArchivesVos", recommendGoodsGoodsArchives);
        model.addAttribute("pathNodeName", pathNodeName);
        model.addAttribute("categoryCode", categoryBannerRequest.getCategoryCode());
        try {
            model.addAttribute("titleName", "类目banner管理");
        } catch (Exception ex) {
            logger.error("类目banner管理加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryBanner/index";
    }

    @RequestMapping(value = "/bannerList.htm")
    public String bannerList(Model model, CategoryBannerRequest categoryBannerRequest) {
        List<CategoryBanner> categoryBanners = iCategoryBannerService.getCategoryBannerByCategoryCode(categoryBannerRequest);
        String pathNodeName = "";
        if (!StringUtils.isEmpty(categoryBannerRequest.getCategoryCode())) {
            pathNodeName = iGoodsCategory.getGoodsCategoryPathNameByGoodsCategoryCode(categoryBannerRequest.getCategoryCode());
        }
        List<RecommendGoodsForSelVo> recommendGoodsGoodsArchives=iRecommendGoodsService.getRecommendGoodsGoodsArchivesByCategoryCode(categoryBannerRequest.getCategoryCode());
        if(CollectionUtils.isEmpty(recommendGoodsGoodsArchives))
            recommendGoodsGoodsArchives= Lists.newArrayList();
        model.addAttribute("categoryBannersVos", categoryBanners);
        model.addAttribute("recommendGoodsGoodsArchivesVos", recommendGoodsGoodsArchives);
        model.addAttribute("pathNodeName", pathNodeName);
        model.addAttribute("categoryCode", categoryBannerRequest.getCategoryCode());
        try {
            model.addAttribute("titleName", "类目banner管理");
        } catch (Exception ex) {
            logger.error("类目banner管理加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryBanner/bannerList";
    }




    /**
     * 获取商品分类树信息
     *
     * @param nodeId 上级节点ID
     * @return 状态
     */
    @RequestMapping(value = "/getGoodsCategoryTreeInfoForFirstLevel.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getGoodsCategoryTreeInfoForFirstLevel(String nodeId) {
        try {
            String treeJson = iGoodsCategory.getGoodsCategoryTreeInfoForFirstLevel();
            return treeJson;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nodeId;
    }

    /**
     * 更新排序值
     *
     * @param categoryBanner 排序信息
     * @return 状态
     */
    @RequestMapping(value = "/updateSortAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult updateSortAction(HttpServletRequest request, CategoryBanner categoryBanner) {
        try {
            if (iCategoryBannerService.updateSort(categoryBanner)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");
    }

    /**
     * 更新排序值
     *
     * @param categoryBanner 排序信息
     * @return 状态
     */
    @RequestMapping(value = "/deleteCategoryBannerAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult deleteCategoryBannerAction(HttpServletRequest request, CategoryBanner categoryBanner) {
        try {
            if (iCategoryBannerService.deleteCategoryBannerByPrimaryKey(categoryBanner.getId())) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");
    }


    @RequestMapping(value = "/bannerDetail.htm")
    public String goodsDetail(Model model, CategoryBannerRequest categoryBannerRequest) {
        logger.info("Go to goodsArchives categoryBanner bannerDetail page...");
        try {
            if (StringUtils.isEmpty(categoryBannerRequest.getId() + "")) {
                categoryBannerRequest.setId(Long.valueOf(0));
            }
            CategoryBanner categoryBanner = iCategoryBannerService.getCategoryBannerModel(categoryBannerRequest.getId());
            if(categoryBanner==null)
            {
                categoryBanner=new CategoryBanner();
                Integer maxSort=iCategoryBannerService.getMaxSortByCategoryCode(categoryBannerRequest);
                categoryBanner.setSort(maxSort+1);
                categoryBanner.setCategoryCode(Long.valueOf(categoryBannerRequest.getCategoryCode()));
                categoryBanner.setId(Long.valueOf(0));
            }
            String pathNodeName = "";
            if (!StringUtils.isEmpty(categoryBannerRequest.getCategoryCode()))
            {
                pathNodeName = iGoodsCategory.getGoodsCategoryPathNameByGoodsCategoryCode(categoryBannerRequest.getCategoryCode());
            }
            model.addAttribute("titleName", "类目banner维护");
            model.addAttribute("pathNodeName", pathNodeName);
            model.addAttribute("categoryBannerVo", categoryBanner);
        } catch (Exception ex) {
            logger.error("类目banner维护加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryBanner/bannerDetail";
    }

    /**
     * 编辑分类banner信息
     *
     * @param categoryBanner 分类banner信息
     * @return 状态
     */
    @RequestMapping(value = "/modifyCategoryBannerAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifyCategoryBannerAction(HttpServletRequest request, CategoryBanner categoryBanner) {
        try {
            if (iCategoryBannerService.modifyCategoryBanner(categoryBanner)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(FAILURE.code, FAILURE.msg);
    }
}
